<?php
include '../main/main.inc.php';
if(!$session->sesssionExists())
	die("Sessione Inesistente");
	
class Event extends event_data 
{
	public function __construct()
	{
		parent::__construct();
		
		switch ($_POST['op'])
		{
			case "AjaxGetEventListProgrammed":
				echo $this->AjaxGetEventListProgrammed();
			break;
			case "AjaxGetEventListExpired":
				echo $this->AjaxGetEventListExpired();
			break;
			case "AjaxGetEvent":
				echo $this->AjaxGetEvent();
			break;
			case "AjaxSetEvent":
				echo $this->AjaxSetEvent();
			break;
			case "AjaxDeleteEvent":
				echo $this->AjaxDeleteEvent();
			break;
		}
	}
	
	public function AjaxGetEventListProgrammed()
	{
		$Event_data = parent::getEventListProgrammed();

		if($Event_data)
		{
			$elEventListGrid = new elSimplyGrid();
			$elEventListGrid->setDataOption('startdate','long-time');
			$elEventListGrid->setDataOption('duedate','long-time');
			$elEventListGrid->setDataOption('enddate','long-time');
			$elEventListGrid->createSimplyGrid('Lista Eventi in Programmazione', 'eventlist_p', array('Nome Evento', 'Istanza', 'Start', 'End', 'Scadenza'), 
				$Event_data,
				el::elButton('elimina_event','Elimina Evento').el::elS().el::elButton('modifica_event', 'Modifica Evento')
			);	
			return $elEventListGrid->elSimplyGrid;
		}
		else
			return null;	
	}
	
	public function AjaxGetEventListExpired() 
	{
		$Event_data = parent::getEventListExpired();
		
		if($Event_data)
		{
			$elEventListGrid = new elSimplyGrid();
			$elEventListGrid->setDataOption('startdate','long-time');
			$elEventListGrid->setDataOption('duedate','long-time');
			$elEventListGrid->setDataOption('enddate','long-time');
			$elEventListGrid->createSimplyGrid('Lista Raid Scaduti', 'eventlist_e', array('Nome Evento', 'Istanza', 'Start', 'End', 'Scadenza'), 
				$Event_data,
				el::elButton('view_event','Consulta Evento')
			);	
			return $elEventListGrid->elSimplyGrid;
		}
		else
			return null;
		
	}
	
	public function AjaxGetEvent() {
		$array = parent::getEvent();
		$startdate = $array['startdate'];
		$array['startdate'] = sys::getdate($startdate,'short');
		$array['starttime'] = sys::getdate($startdate,'ora12');
		$array['endtime'] = ($array['enddate']-$startdate)/60/60;
		$array['duetime'] = ($startdate - $array['duedate'])/60/60;
		return json_encode($array);
	}
	
	public function AjaxSetEvent() {
		if(preg_match('/^[a-zA-Z]/',$_POST['endtime']) or preg_match('/^[a-zA-Z]/',$_POST['duetime']))
		{
			return 'Durata raid e Scadenza iscrizioni possono essere solo valori Numerici';
		}
		else 
		{
			$start = timeUtil::StringToUnix($_POST['startdate'].'-'.$_POST['starttime']);
			$end = $start + ($_POST['endtime']*60*60);
			$due = $start - ($_POST['duetime']*60*60);
			return parent::setEvent($_POST['eventname'],$_POST['istanceid'],$_POST['raidid'],$_POST['comment'], $start, $end, $due, $_POST['onlymain'], $_POST['onlyprimaryspec']);
		}
	}
	
	public function AjaxDeleteEvent() {
		return parent::deleteEvent();
	}
	
	
}

if($session->sesssionExists())
		$event = new Event();
?>